const {src,dest,series,parallel} = require('gulp')
const htmlmin = require('gulp-htmlmin')
const autoprefixer = require('gulp-autoprefixer')
const cleanCss = require('gulp-clean-css')
const uglify = require('gulp-uglify')
const babel = require('gulp-babel')
const imagemin = require('gulp-imagemin')
const webserver = require('gulp-webserver')
//处理html
function handlerHTML(cb){
    src('./project/html/*.html')
    .pipe(htmlmin({
        collapseWhitespace:true
    }))
    .pipe(dest('dist/html'))
    cb()
}
//处理css
function handlerCSS(cb){
    src('./project/css/*.css')
    .pipe(autoprefixer())
    .pipe(cleanCss())
    .pipe(dest('dist/css'))
    cb()
}
//处理sass
function handlerSASS(cb){
    src('./project/sass/*.scss')
    .pipe(autoprefixer())
    .pipe(cleanCss())
    .pipe(dest('dist/css'))
    cb()
}
//js处理
function handlerJS(cb){
    src('./project/js/*.js')
    .pipe(babel({
        presets:["es2015"]
    }))
    .pipe(uglify())
    .pipe(dest('dist/js'))
    cb()
}
//网站图标
function handlerICO(cb){
    src('./project/favicon.ico')
    .pipe(dest('./dist'))
    cb()
}
//图片处理
function handlerIMG(cb){
    src(['./project/images/*.jpg','./project/images/*.png'])
    .pipe(imagemin())
    .pipe(dest('./dist/images'))
    cb()
}

function server(cb){
    src('./project')
    .pipe(webserver({
        proxies:[
            {
                source: '/arise',
                target: 'https://www.baidu.com'
            },
            {
                source: '/ararar',
                target: 'https://www.kiehls.com.cn/api/mall/kiehls/v1/esservice/es/product/getAllProductMap'
            },
            {
                source: '/ararar1',
                target: 'https://www.kiehls.com.cn/api/mall/kiehls/v1/orderservice/cart/getCart'
            },
            {
                source: '/abcd',
                target: 'https://www.kiehls.com.cn/api/mall/kiehls/v1/productservice/product/querySku',

            }
           
        ]
    }))
    cb()
}
//导出
exports.default = series(parallel(handlerICO,handlerJS,handlerCSS,handlerHTML,handlerIMG,handlerSASS),server)